package com.itheima.service.pms.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.exception.CustomException;
import com.itheima.mapper.pms.SkuStockMapper;
import com.itheima.pojo.pms.SkuStock;
import com.itheima.service.pms.SkuStockService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SkuStockServiceImpl extends ServiceImpl<SkuStockMapper, SkuStock> implements SkuStockService {
    /**
     * 批量更新sku库存信息
     *
     * @param skuStockList
     * @param pid
     */
    @Override
    public void adminUpdate(List<SkuStock> skuStockList, Long pid) {
        if (pid == null && skuStockList.size() == 0) {
            throw new CustomException("暂无信息！");
        }
        //构造条件构造器
        LambdaUpdateWrapper<SkuStock> lqw = new LambdaUpdateWrapper<>();
        //pid唯一
        lqw.eq(SkuStock::getProductId, pid);
        //遍历集合
        for (int i = 0; i < skuStockList.size(); i++) {
            //更新每个sku库存信息
            SkuStock skuStock = skuStockList.get(i);
            if (skuStock != null) {
                this.updateById(skuStock);
            }
        }
    }

    /**
     * 根据商品ID及sku编码模糊搜索sku库存
     *
     * @return
     */
    @Override
    public List<SkuStock> selectById(Long pid ,String keyword) {
        //构造条件构造器
        LambdaQueryWrapper<SkuStock> lqw=new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotEmpty(keyword),SkuStock::getSkuCode,keyword);
        lqw.like(SkuStock::getProductId,pid);
        List<SkuStock> skuStockList = this.list(lqw);
        if (skuStockList==null&&skuStockList.size()==0){
            throw new CustomException("暂无数据！");
        }
        return skuStockList;
    }
}
